<?php
class CommonUtility{

	function __construct(){
		
	}

	function __destruct(){

	}

    public static function getVenueTermsAndCancellationPolicy($venue_id = NULL){
        $qry = "select tc_policy,cancellation_policy from venues where id = ".MyDB::$db->qstr($venue_id);
        $rs = MyDB::$db->Execute($qry);
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                $tc_policy = $row['tc_policy'];
                $cancellation_policy = $row['cancellation_policy'];
            }
        }
        return $tc_policy.'##'.$cancellation_policy;
    }

    public function getAllVendorServices($vendor_id = NULL,$sel_vendor_services=array()){
        $status = '1';
        $qry = "select vs.id, v.name  as vendor_name, sm.name as service_name from vendor_services as vs
        INNER JOIN services_master as sm ON sm.id = vs.service_id 
        INNER JOIN vendor as v ON v.id = vs.vendor_id
        where vs.status = ". MyDB::$db->qstr($status);
        $rs = MyDB::$db->Execute($qry);
        $option = '';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                //$selected = ($row['id'] == $sel_id)?'selected':'';
                $selected = in_array($row['id'],$sel_vendor_services)?'selected':'';
                $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['service_name'].' - '.$row['vendor_name'].'</option>';
            }
        }
        return $option;
    }

    public static function getVendorTermsAndCancellationPolicy($vendor_id = NULL){
        $qry = "select tc_policy,cancellation_policy from vendor where id = ".MyDB::$db->qstr($vendor_id);
        $rs = MyDB::$db->Execute($qry);
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                $tc_policy = $row['tc_policy'];
                $cancellation_policy = $row['cancellation_policy'];
            }
        }
        return $tc_policy.'##'.$cancellation_policy;
    }

    public static function getCitiesForState($state,$sel_id=NULL){
        $status = 1;
        $where = " where status = ". MyDB::$db->qstr($status);
        if(!empty($state)) $where.=" AND state_id=". MyDB::$db->qstr($state);
        $qry = "select * from city_master".$where;
        $rs = MyDB::$db->Execute($qry);
        $cities = array();
        if( $rs ){
            while ($row = $rs->fetchRow()) {
                $cities[] = $row;
            }
        }
        return $cities;
    }

    public function getCommonImages($wedding_id,$entity_type){
        $qry =  "SELECT * FROM site_galleries WHERE entity_id=". MyDB::$db->qstr($wedding_id)." AND entity_type = ".MyDB::$db->qstr($entity_type);
        $rs = MyDB::$db->Execute($qry);
        $common_images = array();
        while ($row = $rs->fetchRow()) {
            $common_images[] = $row;
        }
        return $common_images;
    }

    public function getSameCityVenues($city_id){
        $status = '1';
        $qry =  "SELECT id,venue_name, display_name FROM venues WHERE city=". MyDB::$db->qstr($city_id)." AND status = ".MyDB::$db->qstr($status);
        $rs = MyDB::$db->Execute($qry);
        //$option = '<option value="">Select Similar Venues</option>';
        $option = '';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                $selected = ($row['id'] == $sel_id)?'selected':'';
                $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['venue_name'].'</option>';
            }
        }
        return $option;
    }

    public function getVendorServicesByVendorId($vendor_id = NULL,$sel_vendor_services=array()){
        $status = '1';
        echo $qry = "select vs.id, sm.name as service_name from vendor_services as vs
        INNER JOIN services_master as sm ON sm.id = vs.service_id 
        where vs.vendor_id = ".MyDB::$db->qstr($vendor_id)." AND vs.status = ". MyDB::$db->qstr($status);
        $rs = MyDB::$db->Execute($qry);
        //$option = '<option value="">Select Vendor Services</option>';
        $option = '';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                //$selected = ($row['id'] == $sel_id)?'selected':'';
                $selected = in_array($row['id'],$sel_vendor_services)?'selected':'';
                $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['service_name'].'</option>';
            }
        }
        return $option;
    }

    public static function getMaxCapacityByVenueId($venue_id = NULL){
        $qry = "select min_capacity, max_capacity from venues where id = ".MyDB::$db->qstr($venue_id);
        $rs = MyDB::$db->Execute($qry);
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                $min_capacity = $row['min_capacity'];
                $max_capacity = $row['max_capacity'];
            }
        }
        return $min_capacity.'_'.$max_capacity;
    }

    public static function getVenuesByVendorId($vendor_id = NULL){
        $status = '1';
        if(!empty($vendor_id)) $where = " and vendor_id=". MyDB::$db->qstr($vendor_id);
        $qry = "select id,display_name from venues where 1=1 $where AND status = ". MyDB::$db->qstr($status);
        $rs = MyDB::$db->Execute($qry);
        $option = '<option value="">Select Venue</option>';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
                $selected = ($row['id'] == $sel_id)?'selected':'';
                $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['display_name'].'</option>';
            }
        }
        return $option;
    }
	
	public function checkAvailability($table=NULL, $field=NULL, $value=NULL, $col_value=NULL, $col='id'){
		$qry = "SELECT * FROM ".$table." WHERE ".$field." = ".MyDB::$db->qstr($value);
        if(!empty($col_value)){    
            $qry .= " and $col!=".MyDB::$db->qstr($col_value);
        }
		$rs = MyDB::$db->Execute($qry);
		$str = '';
		if($rs->RecordCount() > 0){
			$str = '<div id="error_availability">Already Taken</div>';
			//$str = 1;
		}else{
			$str = '<div id="success_availability">Available</div>';
			//$str = 0;
		}
		return $str;
	}
	
	public function getAttributesByServiceId($id=NULL){
		//$qry = "SELECT sa.id, sa.name FROM services_attributes as sa WHERE sa.id IN(".$attributes.")";
        $qry = "SELECT ca.id, ca.name, sa.required FROM services_attributes sa, category_attributes as ca WHERE ca.id = sa.attribute_id and sa.service_id ='$id'";
		$rs = MyDB::$db->Execute($qry);
		$strcontent = '';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
				if($row['required'] == '1' ){
                    $strcontent.='<p><label>'.$row['name'].'<span class="star">*</span>'.'</label><input type="text" class="input-short required" name="attributes['.$row['id'].']['.$row['name'].']" value=""/></p>';
                }else{
                    $strcontent.='<p><label>'.$row['name'].'</label><input type="text" class="input-short" name="attributes['.$row['id'].']['.$row['name'].']" value=""/></p>';                    
                }
			}
        }
		return $strcontent;
	}
	
	
	public function getAttributesByProductId($id=NULL){
		$qry = "SELECT ca.id, ca.name, pa.required,pa.unit FROM product_attributes pa, category_attributes as ca WHERE ca.id = pa.attribute_id and pa.product_id ='$id'";
		$rs = MyDB::$db->Execute($qry);
		$strcontent = '';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
                if($row['required'] == '1' ){ 
				    $strcontent.='<p><label>'.$row['name'].'<span class="star">*</span>'.'</label><input type="text" class="input-short required" name="attributes['.$row['id'].']['.$row['name'].']" value=""/>&nbsp;&nbsp;'.$row['unit'].'<input type="hidden" name="unit_'.$row['id'].'" value="'.$row['unit'].'"></p>';
                }else{
                    $strcontent.='<p><label>'.$row['name'].'</label><input type="text" class="input-short" name="attributes['.$row['id'].']['.$row['name'].']" value=""/>&nbsp;&nbsp;'.$row['unit'].'<input type="hidden" name="unit_'.$row['id'].'" value="'.$row['unit'].'"></p>';
                }
			}
        }
		return $strcontent;
	}

    public function getAttributesByCategoryId($id=NULL){
        $qry = "SELECT category_id as id, attribute_id, unit_id, required  FROM category_attributes WHERE category_id = '$id'";
        $rs = MyDB::$db->Execute($qry);
        $strcontent = '';
        if( $rs ){
            $parms = null;
            while ($row=$rs->fetchRow()) {                
                $rowAttribute = CommonUtility::getDataById($row['attribute_id'], $table = 'attribute_master');
                $attributeName = $rowAttribute['title'];
                
                $rowUnit = CommonUtility::getDataById($row['unit_id'], $table = 'unit_master');
                $unitName = $rowUnit['title'];
                $star = '';
                if($row['required'] == '1' ){ 
                    $star = '<span class="star">*</span>';
                }
                $strcontent.='<p><label>'.$attributeName.$star .'</label><input type="text" class="input-short required" name="attributes['.$row['attribute_id'].']['.$attributeName.']" value=""/>&nbsp;&nbsp;'.$unitName.'<input type="hidden" name="unit_'.$row['attribute_id'].'" value="'.$row['unit_id'].'"></p>';
                
            }
        }
        return $strcontent;
    }
    
    public function getCategoryAttribute($id=NULL, $service_id){
        $qry = "SELECT * FROM category_attributes WHERE category_id=".MyDB::$db->qstr($id);
        $rs = MyDB::$db->Execute($qry);
        $strcontent = '';
        if( $rs ){
            $qry = "SELECT * FROM services_attributes WHERE status='1' and service_id=".MyDB::$db->qstr($service_id);;
            $rsprod = MyDB::$db->Execute($qry);            
            while ($rowProd = $rsprod->fetchRow()) {                
                $rowProduct[$rowProd['attribute_id']]['required'] = $rowProd['required'];
                $rowProduct[$rowProd['attribute_id']]['unit'] = $rowProd['unit'];
            }
            
            $qry = "SELECT * FROM unit_master WHERE status='1'";
            $rsunit = MyDB::$db->Execute($qry);
            $unit = '<option value="">No Unit</option>';
            while ($rowUnit = $rsunit->fetchRow()) {                
                $unit .= '<option value="'.$rowUnit['title'].'">'.$rowUnit['title'].'</option>';
            }
            while ($row=$rs->fetchRow()) {                
                $strcontent.='<tr><td style="border:0 none;">'.$row['name'].'</td>';
                if( $rowProduct[$row['id']] ){                    
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" checked id="attributes" name="attributes[]" value="'.$row['id'].'" /></td>';
                    $sel = $rowProduct[$row['id']]['required'] == '1' ? 'checked' : '';
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" '.$sel.' name="comp_'.$row['id'].'"></td>';
                    $tmpunit = $unit;
                    if(!empty($rowProduct[$row['id']]['unit']))
                         $tmpunit = str_replace('>'.$rowProduct[$row['id']]['unit'],'selected >'.$rowProduct[$row['id']]['unit'],$unit);
                    $strcontent.='<td style="border:0 none;"><select class="input-medium" name="unit_'.$row['id'].'" >'.$tmpunit.'</select></td></tr>';
                }else{                    
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" id="attributes" name="attributes[]" value="'.$row['id'].'" /></td>';
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" name="comp_'.$row['id'].'"></td>';
                    $strcontent.='<td style="border:0 none;"><select class="input-medium" name="unit_'.$row['id'].'" >'.$unit.'</select></td></tr>';
                }
                
            }
            
            $strcontent = '<table style="width:500px;border:0 none;"><tr><td style="border:0 none;"><strong>Attribute</strong></td><td style="border:0 none;"><strong>Add</strong></td><td style="border:0 none;"><strong>Compulsary</strong></td><td style="border:0 none; width:220px;"><strong>Unit</strong></td></tr>'.$strcontent.'</table>';
        }
        return $strcontent;
    }

    public function getProdAttribute($id=NULL, $prod_id){
        $qry = "SELECT * FROM category_attributes WHERE category_id=".MyDB::$db->qstr($id);
        $rs = MyDB::$db->Execute($qry);
        $strcontent = '';
        if( $rs ){
            $qry = "SELECT * FROM product_attributes WHERE status='1' and product_id=".MyDB::$db->qstr($prod_id);;
            $rsprod = MyDB::$db->Execute($qry);            
            while ($rowProd = $rsprod->fetchRow()) {                
                $rowProduct[$rowProd['attribute_id']]['required'] = $rowProd['required'];
                $rowProduct[$rowProd['attribute_id']]['unit'] = $rowProd['unit'];
            }
            $qry = "SELECT * FROM unit_master WHERE status='1'";
            $rsunit = MyDB::$db->Execute($qry);
            $unit = '<option value="">No Unit</option>';
            while ($rowUnit = $rsunit->fetchRow()) {                
                $unit .= '<option value="'.$rowUnit['title'].'">'.$rowUnit['title'].'</option>';
            }
            
            while ($row=$rs->fetchRow()) {                
                $strcontent.='<tr><td style="border:0 none;">'.$row['name'].'</td>';
                if( $rowProduct[$row['id']] ){                    
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" checked id="attributes" name="attributes[]" value="'.$row['id'].'" /></td>';
                    $sel = $rowProduct[$row['id']]['required'] == '1' ? 'checked' : '';
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" '.$sel.' name="comp_'.$row['id'].'"></td>';
                    $tmpunit = $unit;
                    if(!empty($rowProduct[$row['id']]['unit']))
                         $tmpunit = str_replace('>'.$rowProduct[$row['id']]['unit'],'selected >'.$rowProduct[$row['id']]['unit'],$unit);
                    $strcontent.='<td style="border:0 none;"><select class="input-medium" name="unit_'.$row['id'].'" >'.$tmpunit.'</select></td></tr>';
                }else{                    
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" id="attributes" name="attributes[]" value="'.$row['id'].'" /></td>';
                    $strcontent.='<td style="border:0 none;"><input type="checkbox" name="comp_'.$row['id'].'"></td>';
                    $strcontent.='<td style="border:0 none;"><select class="input-medium" name="unit_'.$row['id'].'" >'.$unit.'</select></td></tr>';
                }
            }
            
            $strcontent = '<table style="width:500px;border:0 none;"><tr><td style="border:0 none;"><strong>Attribute</strong></td><td style="border:0 none;"><strong>Add</strong></td><td style="border:0 none;"><strong>Compulsary</strong></td><td style="border:0 none; width:220px;"><strong>Unit</strong></td></tr>'.$strcontent.'</table>';
        }
        return $strcontent;
    }

    public static function getData($parms=null, $table, $condition=null){
            
        $qry = "select * from $table ";
        if( !empty($condition) ){
            foreach( $condition as $key => $val ){
                $tmpArr[] = $key .'='. MyDB::$db->qstr($val);                
            }
            $qry = $qry . ' where '.implode(' && ', $tmpArr);
        }
        if( $parms ){      
             $limitClaus = '';
             if( $parms['paging'] > 0 && $parms['count']<>1 ){
                $perpage = $parms['pagecount'] > 0 ? $parms['pagecount'] : 20;
                $limit =  ($parms['paging']-1)*$perpage;
                $limitClaus = " limit $limit, $perpage ";
             }              
        }
        
        //$qry = "select * from admin where username=".MyDB::$db->qstr($uname)." and password=".MyDB::$db->qstr($pass)." ";
        $qry .= $limitClaus;
        $rs = MyDB::$db->Execute($qry);
        if( $rs ){
            if( $parms['count']==1) return $rs->RecordCount();
            
            $arr = $rs->GetArray();
            return $arr;
        }
        return false;
    }             
    
    public static function getDataById($id, $table, $condition=null){
        if( $id>0 ){
            $qry = "select * from $table where id=".MyDB::$db->qstr($id);
            if( !empty($condition) ){
                foreach( $condition as $key => $val ){
                    $tmpArr[] = $key .'='. MyDB::$db->qstr($val);                
                }
                $qry = $qry . ' && '.implode(' && ', $tmpArr);
            }
            
            $rs = MyDB::$db->Execute($qry);  
            if( $rs ){
                $arr = $rs->fetchRow();
                return $arr;                
            }
        }
        return false;     
    }   
    
    public static function insertData($parms, $table, $flag=NULL){
        if( $table && $parms)    {
            $qry = "insert into $table set ";
            $insStr = array();
            foreach($parms as $key => $val){
                    $insStr[] = $key .'='. MyDB::$db->qstr($val);
            }
            $qry = $qry . implode(',', $insStr);
            $rs = MyDB::$db->Execute($qry);
            if( $rs ){
                if($flag != ''){
					$id = mysql_insert_id();
					return $id;
				}else{
					return true;
				}
            }
            return false;
        }        
    } 
    
     public static function updateData($parms, $table, $condition=null, $operator=' && '){
        if( $table && $parms)    {
            $qry = "update $table set ";
            $tmpArr = array();
            foreach($parms as $key => $val){
                    $tmpArr[] = $key .'='. MyDB::$db->qstr($val);
            }
            $qry = $qry . implode(',', $tmpArr);
            $tmpArr = array();
            if( !empty($condition) ){
                foreach( $condition as $key => $val ){
                    $tmpArr[] = $key .'='. MyDB::$db->qstr($val);                
                }
                $qry = $qry . ' where '.implode($operator, $tmpArr);
            }
            $rs = MyDB::$db->Execute($qry);
            if( $rs ){                
                return true;
            }
            return false;
        }        
    }             
    
    //delete data from specified table  //
    // data will be deleted only if delete condition is provided .. 
    public static function deleteData( $table, $condition=null){
        if( $table && $condition)    {
            $qry = "delete from $table ";
            $tmpArr = array();            
            if( !empty($condition) ){
                foreach( $condition as $key => $val ){
                    $tmpArr[] = $key .'='. MyDB::$db->qstr($val);                
                }
                $qry = $qry . ' where '.implode(' && ', $tmpArr);
                $rs = MyDB::$db->Execute($qry);
                if( $rs ){                
                    return true;
                }
            }            
            return false;
        }        
    }            
    public static function getCountry($sel_id=NULL){
		$status = 1;
		$qry = "select * from country_master where status = ". MyDB::$db->qstr($status);
		$rs = MyDB::$db->Execute($qry);
		$option = '<option value="">--Select Country--</option>';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
				$selected = ($row['id'] == $sel_id)?'selected':'';
			    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
			}
        }
        return $option;
	}
	public static function getState($country,$sel_id=NULL){
		$status = 1;
		$where = " where status = ". MyDB::$db->qstr($status);
		if(!empty($country)) $where.=" AND country_id=". MyDB::$db->qstr($country);
		$qry = "select * from state_master".$where;
		$rs = MyDB::$db->Execute($qry);
        $option .= '<option value="">--Select State--</option>';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
				$selected = ($row['id'] == $sel_id)?'selected':'';
				$option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
			}
        }
        return $option;
	}
	public static function getCity($state,$sel_id=NULL){
		$status = 1;
		$where = " where status = ". MyDB::$db->qstr($status);
		if(!empty($state)) $where.=" AND state_id=". MyDB::$db->qstr($state);
		$qry = "select * from city_master".$where;
		$rs = MyDB::$db->Execute($qry);
		//$option .= '<option value="">--Select City--</option>';
        if( $rs ){
            if( is_array($sel_id) ){
                while ($row=$rs->fetchRow()) {
				    $selected = in_array($row['id'], $sel_id) ? 'selected' : '';
                    //$selected = ($row['id'] == $sel_id)?'selected':'';
				    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
			    }
            }else{
                while ($row=$rs->fetchRow()) {
                    $selected = ($row['id'] == $sel_id)?'selected':'';
                    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
                }
            }
        }
        return $option;
	}
    
    //  default formating is like 12 dev 2011
    public static function formatDate($dt, $type=null){
        date_default_timezone_set('Asia/Calcutta');
        switch($type){
            case 1:
                
                break;
            
            default : 
                $dt = date('d M Y', @strtotime($dt));                
        }    
        return $dt;
    }
    
    public static function uploadNcreateThumb($file, $module, $thumbFlag, $thumbSize=100, $custom_size=false){ 
        global $_img_upload_path;       //  global variable contains path for uploading images on website
        if(count($_FILES)){
            if(is_array($_FILES[$file]['error']) && count($_FILES[$file]['error'])>=1) {         //multiple file upload request
                $uploadFileNames = array();
                foreach($_FILES[$file]['error'] as $ind => $errval ){
                    if( $errval==0 ){           //  upload image
                        
                        $imagename = basename($_FILES[$file]['name'][$ind]);
                        $tmp = explode('.',basename($imagename));
                        $ct = count($tmp);
                        $ext = '';
                        if( $ct >= 1 ){
                            $ext = '.'.$tmp[$ct-1];
                        }
                        $filename = time().'_'.$ind.$ext;                
                        $uploadDir = _SITE_PATH_ . $_img_upload_path[$module];
                        $uploadfile = $uploadDir . $filename;   //basename($_FILES['myimage']['name']);
                        
                        if (move_uploaded_file($_FILES[$file]['tmp_name'][$ind], $uploadfile)) {  
                            //  image moved successfully //
                            if( $thumbFlag ){
                                $imageName = $filename;                                      
                                CommonUtility::createThumb($imageName, $module, $thumbSize);
                            }
                            $uploadFileNames[] = $filename;
                        } else {
                           //echo "Possible file upload attack!\n";
                           //return -1;
                        }                        
                    }                    
                } 
                if( !empty($uploadFileNames )){ return $uploadFileNames;}
                return -1;
                
            }
            else if( $_FILES[$file]['error']==0 ){
                $imagename = basename($_FILES[$file]['name']);
                $tmp = explode('.',basename($imagename));
                $ct = count($tmp);
                $ext = '';
                if( $ct>1 ){
                    $ext = '.'.$tmp[$ct-1];
                }
                $filename = time().$ext;                
                $uploadDir = _SITE_PATH_ . $_img_upload_path[$module];
                $uploadfile = $uploadDir . $filename;   //basename($_FILES['myimage']['name']);
                
                if (move_uploaded_file($_FILES[$file]['tmp_name'], $uploadfile)) {  
                    //  image moved successfully //
                    if( $thumbFlag ){
                        $imageName = $filename; 
                        if($custom_size){
                            CommonUtility::createThumb($imageName, $module, THUMB_SIZE_CUSTOM);
                            CommonUtility::createThumb($imageName, $module, SMALL_SIZE, SMALL_DIR);
                            CommonUtility::createThumb($imageName, $module, BIG_SIZE, BIG_DIR);
                        }else{
                            CommonUtility::createThumb($imageName, $module, $thumbSize);
                        }
                        
                    }
                    return $filename;
                } else {
                   //echo "Possible file upload attack!\n";
                   return -1;
                }
            }else{
                //echo 'Uploading Failed';
                return -1;
            }
        }
    }
 
    public static function createThumb($imgName, $module, $size, $thumb_dir=THUMB_DIR){        
            global $_img_upload_path;
            require_once _SITE_PATH_.'lib/CreateThumbnail/function_createthumb.php';
            $uploadDir = _SITE_PATH_ . $_img_upload_path[$module];
            
            $thumbDir = $uploadDir.$thumb_dir;
                                    
            $sourceFile = $uploadDir . $imgName;
            $saveFilePath = $thumbDir.$imgName;                        
            //echo $sourceFile, $size, $saveFilePath;exit;
            if( is_file($sourceFile) && $size>0 ){
                create_thumb($sourceFile, $size, $saveFilePath);
            }
    }
    
    public static function removeImage($filename, $module){
        global $_img_upload_path;
        $uploadDir = _SITE_PATH_ . $_img_upload_path[$module];
        $sourceFile = $uploadDir . $filename;
        //if(is_file($sourceFile)){
            @unlink($sourceFile);                                       //  remove main file
            $thumbDir = $uploadDir.THUMB_DIR;            
            $saveFilePath = $thumbDir.$filename;                        //  remove thumb file
            @unlink($saveFilePath);            
        //}
    }
	
	 public static function getSubCategory($id){
		if(!empty($id)) $where = " where parent_id=". MyDB::$db->qstr($id);
		$qry = "select * from category ".$where;
		$rs = MyDB::$db->Execute($qry);
        $option .= '<option value="">Select Sub Category</option>';
        if( $rs ){
            while ($row=$rs->fetchRow()) {
			 $option .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
			}
        }
        return $option;
	}
	public static function getZone($sel_id=NULL){
		$status = 1;
		$qry = "select * from zone_master where status = ". MyDB::$db->qstr($status);
		$rs = MyDB::$db->Execute($qry);
		$option = '<option value="">Select Zone</option>';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
				$selected = ($row['id'] == $sel_id)?'selected':'';
			    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['zone'].'</option>';
			}
        }
        return $option;
	}
	public static function getServiceAttributes($arrServiceAttributes=array()){
		$status = 1;
		$qry = "select * from services_attributes where status = ". MyDB::$db->qstr($status);
		$rs = MyDB::$db->Execute($qry);
		$option = '<option value="">--Select Attributes--</option>';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
				$selected = in_array($row['id'],$arrServiceAttributes)?'selected':'';
			    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
			}
        }
        return $option;
	}
	public static function getProductAttributes($arrProductAttributes=array()){
		$status = 1;
		$qry = "select * from category_attributes where status = ". MyDB::$db->qstr($status);
		$rs = MyDB::$db->Execute($qry);
		$option = '<option value="">--Select Attributes--</option>';
        if( $rs ){
			while ($row=$rs->fetchRow()) {
				$selected = in_array($row['id'],$arrProductAttributes)?'selected':'';
			    $option .= '<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
			}
        }
        return $option;
	}

	public static function updateOnDuplicate($parms, $table){
		if( $table && $parms)    {
            $qry = "insert into $table set ";
            $insStr = array();
            foreach($parms as $key => $val){
                    $insStr[] = $key .'='. MyDB::$db->qstr($val);
            }
            $qry = $qry . implode(',', $insStr);
            
            $qry = $qry . ' ON DUPLICATE KEY UPDATE ' . implode(',', $insStr);
            $rs = MyDB::$db->Execute($qry);
            if( $rs ){                
				return true;				
            }
            return false;
        }
	}
	
	public static function includeEditor( $editorConfi=null, $finderConfig=null ){
		
		include_once _RESOURCE_PATH.'ckeditor/ckeditor.php';
		include_once _RESOURCE_PATH.'ckfinder/ckfinder.php';
		
		$ckeditor = new CKEditor();
		$ckeditor->basePath = _SITE_BASE_PATH.'resources/ckeditor/';
		$ckfinder = new CKFinder();
		$ckfinder->BasePath = _SITE_BASE_PATH.'/resourcesckfinder/'; // Note: the BasePath property in the CKFinder class starts with a capital letter.
		$ckfinder->SetupCKEditorObject($ckeditor);
		$ckeditor->config['toolbar'] = array(
				array( 'Source','-',
						'Preview','Templates','-',
						'Cut','Copy','Paste','PasteText','-',
						'Undo','Redo','-',
						'Find','Replace','-',
						'SelectAll','RemoveFormat','-',
						'Maximize', 'ShowBlocks'),
				'/',
				array('Bold','Italic','Underline','Strike','-',
						'Subscript','Superscript','-',
						'NumberedList','BulletedList','-',
						'Outdent','Indent','-',
						'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
						'Link','Unlink','Anchor','-',
						'Image','Table','HorizontalRule','SpecialChar'
				),
				'/',
				array('Format','Font','FontSize','-',
						'TextColor','BGColor')
		);
		$ckeditor->config['width'] = 600;
		$ckeditor->config['height'] = 150;
		
		return $ckeditor;
		
	}
}
?>